
// 使用class类封装请求，Class类便于服用和修改

class wxRequest {

    default = {
        baseURL: '',
        url: '',
        data: null,
        method: 'GET',
        header: {
            'Content-type': 'application/json'
        },
        timeout: 6000,

    }
    //定义拦截器对象，包含请求拦截器和响应拦截器，方便在请求之前处理一些数据和响应后的处理
    interceptors = {
        request: (config) => {
            return config
        },
        response: (response) => {
            return response
        }
    }
    constructor(params = {}) {
        this.default = Object.assign({}, this.default, params)
    }
    request(options) {

        options.url = this.default.baseURL + options.url
        options = { ...this.default, ...options }

        options = this.interceptors.request(options)
        wx.showLoading({
            title: '加载中',
        })
        return new Promise((resolve, reject) => {
            if (options.method === 'UPLOAD') {
                wx.hideLoading()
                wx.uploadFile({
                    ...options,
                    success: (res) => {
                        res.data = JSON.parse(res.data)

                        const mergeRes = Object.assign({}, res, {
                            config: options,
                            isSuccess: true
                        })

                        resolve(this.interceptors.response(mergeRes))
                    },
                    fail: (err) => {
                        const mergeErr = Object.assign({}, err, {
                            config: options,
                            isSuccess: false
                        })
                        reject(this.interceptors.response(mergeErr))
                    }
                })
            } else {
                wx.request({
                    ...options,
                    success: (res) => {
                        const mergeRes = Object.assign({}, res, { config: options, isSuccess: true })
                        resolve(this.interceptors.response(mergeRes))
                    },
                    fail: (res) => {
                        const mergeErr = Object.assign({}, err, { config: options, isSuccess: false })
                        resolve(this.interceptors.response(mergeErr))
                    },
                    complete: () => {
                        wx.hideLoading()
                    }
                })

            }
        })
    }

    get(url, data, config) {
        return this.request(Object.assign({ url, data, method: 'GET' }, config))
    }

    delete(url, data, config) {
        return this.request(Object.assign({ url, data, method: 'DELETE' }, config))
    }

    put(url, data, config) {
        return this.request(Object.assign({ url, data, method: 'PUT' }, config))
    }

    post(url, data, config) {
        return this.request(Object.assign({ url, data, method: 'POST' }, config))
    }
    // 封装并发请求，一次发送多个请求，不会造成页面堵塞，提升响应速度和用户体验
    all(...promise) {
        return Promise.all(promise)
    }
    //封装上传图片的实例方法
    upload(url, filePath, name = 'file', config = {}) {
        return this.request(
            Object.assign({ url, filePath, name, method: 'UPLOAD' }, config)
        )
    }
}
export default wxRequest





